capture log close
clear all
set maxvar 30000
set more off
pause off
# delimit;

local crime "";
local data "";

/* 
Sample of SSI children 1990-2012, prep 3
*/

local exp "allssr";
local firstage 18 ;		/* Choose 2009 here because oldest kids turn 18 in 2010 */
local lastyr 2019 ;		/* Last year of crime data */
local lastearnyr 2019 ; /* Last year of earnings data */
local firststockyr 1997 ; /* first year of "stock" variables */
local laststockyr 2019 ; /* last year of "stock" variables */
local range "18(2)22" ;


local first 1994 ;
local last 2016 ;
local thresh = 0.8 ;
local per_range "2(2)22" ;
local cum_range "20(2)40" ;
include "`do'\crime_statecntyselect_allssr.do" ;

use "`crime'\prep2\crime_allssr_remerge.dta", clear;

/* ******************************************** */
/* **** EARNINGS VARIABLES FOR DEMO GROUPS **** */
/* ******************************************** */
forval age = 12(1)17 { ;
	gen temp_hhearn`age' = . ;
	forval yr = 1985(1)2012 { ;
		replace temp_hhearn`age' = n_hhearns_`yr' if `yr' == dobyy + `age' ; 
	} ;
	summ temp_hhearn`age' ;
} ;

egen n_earnhhpre1217=rowmean(temp_hhearn12-temp_hhearn17) ;
summ n_earnhhpre1217 ;


/* Determine county-years that make up denominator */
rename pdscc pdscc_full ;
gen pdscc = substr(pdscc_full,1,5) ;
gen temp_st = substr(pdscc_full,1,2) ;
tab temp_st, m ;
drop temp_st pdscc_full ;

merge m:1 pdscc using `cnty_year', keepusing(keep_* fips) ;
drop if _merge == 2 ;
drop _merge ;
foreach var of varlist keep* { ;
	replace `var' = 0 if `var' == . ;
} ;

summ keep* ;

/* ******************************** */
/* **** DEFINE CRIME VARIABLES **** */
/* ******************************** */

local char_cond 	2 ;
local drug_cond 	3 ;
local traf_cond     4 ;
local fel_cond		5 ;
local genr_cond		6 ;
local nogn_cond  	7 ;
local prop_cond 	8 ;
local pord_cond     9 ;
local viol_cond 	10 ;
local wknd_cond		11 ;
local fcvn_cond		12 ;
local acvn_cond     13 ;
local inca_cond 	14 ;
local par_cond 		15 ;
local prob_cond 	16 ;

local dgsl_cond 1 ;
local dgot_cond 2 ;
local dgps_cond 3 ;
local polq_cond	4 ;
local poot_cond	5 ;
local povc_cond	6 ;
local povl_cond	7 ;
local powp_cond	8 ;
local prbg_cond 9 ;
local prfd_cond 10 ;
local prtf_cond 11 ;
local vrob_cond 12 ;
local vlct_cond 13 ;
local vlpa_cond 14 ;
local vlpr_cond 15 ;

local nias_cond 1 ; 
local nide_cond 2 ; 
local nidc_cond 3 ; 
local nidp_cond 4 ; 
local nidu_cond 5 ; 
local nilq_cond 6 ; 
local niob_cond 7 ; 
local nisx_cond 8 ; 
local nitf_cond 9 ; 
local nitp_cond 10 ; 
local niwp_cond 11 ;	

local any_abbr		"any" ;
local char_abbr 	"2c" ;
local drug_abbr 	"3cd" ;
local traf_abbr 	"4ct" ;
local fel_abbr  	"5cf" ;
local genr_abbr 	"6cg" ;
local nogn_abbr     "7cn" ;
local prop_abbr 	"8cp" ;
local pord_abbr     "9co" ;
local viol_abbr 	"10cv" ;
local wknd_abbr 	"11cw" ;
local fcvn_abbr 	"12fc" ;
local acvn_abbr     "13fm" ;
local inca_abbr		"14ie" ;	
local par_abbr		"15pa" ;
local prob_abbr		"16pr" ;
local ipp_abbr		"ipp" ;

foreach stub in 
	dgsl dgot dgps polq poot povc povl powp prbg prfd prtf vrob vlct vlpa vlpr 
	nias nide nidc nidp nidu nilq niob nisx nitf nitp niwp
	{ ;
		local `stub'_abbr "``stub'_cond'`stub'" ;
	} ;

local type_list_main "char fcvn" ; 
local type_list_main2 "genr nogn drug viol prop pord fel acvn inca" ;
local type_list_bk "dgsl dgot dgps polq poot povc povl powp prbg prfd prtf vrob vlct vlpa vlpr" ;
local type_list_nobk "nias nide nidc nidp nidu nilq niob nisx nitf nitp niwp" ;
local type_list "`type_list_main'" ; 

foreach stub in `type_list_main' { ;
	local `stub'_event "event" ;
} ;
foreach stub in `type_list_bk' { ;
	local `stub'_event "bk_event" ;
} ;
foreach stub in `type_list_nobk' { ;
	local `stub'_event "nobk_event" ;
} ;

forval i=1(1)9 { ;
	rename event_date0`i' event_date`i' ;
	rename event0`i' event`i' ;
	rename day_of_week0`i' day_of_week`i' ;
	rename event_state0`i' event_state`i' ;
} ;

foreach class in bk nobk { ;
	forval i=1(1)9 { ;
		rename `class'_event_date0`i' 	`class'_event_date`i' ;
		rename `class'_event0`i' 		`class'_event`i' ;
		rename `class'_event_state0`i' 	`class'_event_state`i' ;
	} ;
} ;

forval i=1(1)20 { ;
	tostring(event`i'), replace format(%18.0g) ;
	forval j = 3(1)9 { ;
		replace event`i' = subinstr(event`i',"`j'","2",.) ;
	} ;
} ;

foreach class in bk nobk { ;
	forval i=1(1)20 { ;
		recast double `class'_event`i' ;
		tostring(`class'_event`i'), replace format(%19.0g) ;
		forval j = 3(1)9 { ;
			replace `class'_event`i' = subinstr(`class'_event`i',"`j'","2",.) ;
		} ;
	} ;
} ;

summ dobyy ;
local yob_min = r(min) ;
local yob_max = r(max) ;

rename indiv_match temp1 ;
gen byte temp1_part1 = (temp1 == 1 & part == 1) ;

gen byte temp2 = (event_date1 != .) ;

foreach suffix in 1 2 { ;
	gen n_crime`suffix'_18plus = . ;
	
	forval yob=`yob_min'(1)`yob_max' { ;
		if `=`yob'+18' <= `lastyr' { ;
			replace n_crime`suffix'_18plus = temp`suffix' if keep_`=`yob'+18'post_any == 1 & dobyy == `yob' ;
		} ;
	} ;
	
} ;
drop temp* ;


/* **** ANY CRIME AT DIFFERENT AGES **** */
forval age = 18(1)18 { ;			
	local list_`age' " " ;
	forval per = 4(1)4 { ;			
		if `=`age'+`per'' <= 27 { ;
			local list_`age' " `list_`age'' `=`age'+`per'' " ;
		} ;
	} ;
	display `list_`age'' ;
} ;


/* Indicator 18+ */
gen n_crime2any_18plus = . ;

forval yob=`yob_min'(1)`yob_max' { ;

	if `=`yob'+18' <= `lastyr' { ;
	
		replace n_crime2any_18plus = 0 
				if (keep_`=`yob'+18'post_any == 1) & dobyy == `yob' ;
		forval i=1(1)20 { ;
			replace n_crime2any_18plus = 1 
				if (keep_`=`yob'+18'post_any == 1)
				& year(event_date`i' ) >= `=`yob'+18' & year(event_date`i' ) < . & dobyy == `yob';
		} ;		
		
	} ; /*end if */
} ; /* end yob */

/* Total 18+ */
gen n_crime2anyt_18plus = . ;

forval yob=`yob_min'(1)`yob_max' { ;

	if `=`yob'+18' <= `lastyr' { ;
	
		replace n_crime2anyt_18plus = 0 
				if (keep_`=`yob'+18'post_any == 1) & dobyy == `yob' ;
		forval i=1(1)20 { ;
			replace n_crime2anyt_18plus = n_crime2anyt_18plus + 1 
				if (keep_`=`yob'+18'post_any == 1)
				& year(event_date`i' ) >= `=`yob'+18' & year(event_date`i' ) < . & dobyy == `yob';
		} ;		
		
	} ; /*end if */
} ; /* end yob */


forval start = `range' { ;
	foreach end in `list_`start'' { ;	
		
		/* Indicator 18-22 */
		gen n_crime2any_`start'_`end'= . ;
		
		forval yob=`yob_min'(1)`yob_max' { ;

			if `=`yob'+`end'' <= `lastyr' { ;
			
				replace n_crime2any_`start'_`end'= 0 
						if (keep_`=`yob'+`start''_`=`yob'+`end''_any == 1) & dobyy == `yob' ;
				forval i=1(1)20 { ;
					replace n_crime2any_`start'_`end' = 1 
						if (keep_`=`yob'+`start''_`=`yob'+`end''_any == 1)
						& year(event_date`i' ) >= `=`yob'+`start'' & year(event_date`i' ) <= `=`yob'+`end'' & dobyy == `yob';
				} ;		
				
			} ; /*end if */
		} ; /* end yob */
					
		
		/* Total */
		gen n_crime2anyt_`start'_`end'= . ;
		
		forval yob=`yob_min'(1)`yob_max' { ;

			if `=`yob'+`end'' <= `lastyr' { ;
			
				replace n_crime2anyt_`start'_`end'= 0 
						if (keep_`=`yob'+`start''_`=`yob'+`end''_any == 1) & dobyy == `yob' ;
				forval i=1(1)20 { ;
					replace n_crime2anyt_`start'_`end' = n_crime2anyt_`start'_`end' + 1 
						if (keep_`=`yob'+`start''_`=`yob'+`end''_any == 1)
						& year(event_date`i' ) >= `=`yob'+`start'' & year(event_date`i' ) <= `=`yob'+`end'' & dobyy == `yob';
				} ;		
			
			} ; /*end if */
		} ; /* end yob */
		
	} ;	/* end end */
} ;	/* end start */


/* Cumulative at each age */
forval age = `cum_range' { ;
	
		gen n_crime2any_cum`age' = . ;
		
		forval yob=`yob_min'(1)`yob_max' { ;

			if `=`yob'+`age'' <= `lastyr' { ;
			
				replace n_crime2any_cum`age' = 0 
						if (keep_`=`yob'+18'_`=`yob'+`age''_any == 1) & dobyy == `yob' ;
				forval i=1(1)20 { ;
					replace n_crime2any_cum`age' = 1 
						if (keep_`=`yob'+18'_`=`yob'+`age''_any == 1)
						& year(event_date`i' ) >= `=`yob'+18' & year(event_date`i' ) <= `=`yob'+`age'' & dobyy == `yob';
				} ;		
				
			} ; /*end if */
		} ; /* end yob */
					
		
		/* Total */
		gen n_crime2anyt_cum`age' = . ;
		
		forval yob=`yob_min'(1)`yob_max' { ;

			if `=`yob'+`age'' <= `lastyr' { ;
			
				replace n_crime2anyt_cum`age' = 0 
						if (keep_`=`yob'+18'_`=`yob'+`age''_any == 1) & dobyy == `yob' ;
				forval i=1(1)20 { ;
					replace n_crime2anyt_cum`age' = n_crime2anyt_cum`age' + 1 
						if (keep_`=`yob'+18'_`=`yob'+`age''_any == 1)
						& year(event_date`i' ) >= `=`yob'+18' & year(event_date`i' ) <= `=`yob'+`age'' & dobyy == `yob';
				} ;		
			
			} ; /*end if */
		} ; /* end yob */
		
} ; /* end age */	


/* **** SPECIFIC TYPES OF CRIME **** */
foreach type in `type_list' { ;
	
		/* Indicator 18+ */
		gen n_crime2`type'_18plus = . ;
		
		forval yob=`yob_min'(1)`yob_max' { ;

			if `=`yob'+18' <= `lastyr' { ;
			
				replace n_crime2`type'_18plus = 0 
						if (keep_`=`yob'+18'post_``type'_abbr' == 1) & dobyy == `yob' ;
				forval i=1(1)20 { ;
					replace n_crime2`type'_18plus = 1 
						if (keep_`=`yob'+18'post_``type'_abbr' == 1)
						& year(``type'_event'_date`i' ) >= `=`yob'+18' & year(``type'_event'_date`i' ) < . & dobyy == `yob'
						& substr(``type'_event'`i',``type'_cond',1) == "2" ;
				} ;		
				
			} ; /*end if */
		} ; /* end yob */
		
		/* Total 18+ */
		gen n_crime2`type't_18plus = . ;
		
		forval yob=`yob_min'(1)`yob_max' { ;

			if `=`yob'+18' <= `lastyr' { ;
			
				replace n_crime2`type't_18plus = 0 
						if (keep_`=`yob'+18'post_``type'_abbr' == 1) & dobyy == `yob' ;
				forval i=1(1)20 { ;
					replace n_crime2`type't_18plus = n_crime2`type't_18plus + 1 
						if (keep_`=`yob'+18'post_``type'_abbr' == 1)
						& year(``type'_event'_date`i' ) >= `=`yob'+18' & year(``type'_event'_date`i' ) < . & dobyy == `yob'
						& substr(``type'_event'`i',``type'_cond',1) == "2" ;
				} ;		
				
			} ; /*end if */
		} ; /* end yob */
	
	
		forval start = `range' { ;
			foreach end in `list_`start'' { ;	
				
				/* Indicator */
				gen n_crime2`type'_`start'_`end'= . ;
				
				forval yob=`yob_min'(1)`yob_max' { ;

					if `=`yob'+`end'' <= `lastyr' { ;
					
						replace n_crime2`type'_`start'_`end'= 0 
								if (keep_`=`yob'+`start''_`=`yob'+`end''_``type'_abbr' == 1) & dobyy == `yob' ;
						forval i=1(1)20 { ;
							replace n_crime2`type'_`start'_`end' = 1 
								if (keep_`=`yob'+`start''_`=`yob'+`end''_``type'_abbr' == 1)
								& year(``type'_event'_date`i' ) >= `=`yob'+`start'' & year(``type'_event'_date`i' ) <= `=`yob'+`end'' & dobyy == `yob'
								& substr(``type'_event'`i',``type'_cond',1) == "2" ;
						} ;		
						
					} ; /*end if */
				} ; /* end yob */
				
				/* Total */
				gen n_crime2`type't_`start'_`end'= . ;
				
				forval yob=`yob_min'(1)`yob_max' { ;

					if `=`yob'+`end'' <= `lastyr' { ;
					
						replace n_crime2`type't_`start'_`end'= 0 
								if (keep_`=`yob'+`start''_`=`yob'+`end''_``type'_abbr' == 1) & dobyy == `yob' ;
						forval i=1(1)20 { ;
							replace n_crime2`type't_`start'_`end' = n_crime2`type't_`start'_`end' + 1 
								if (keep_`=`yob'+`start''_`=`yob'+`end''_``type'_abbr' == 1)
								& year(``type'_event'_date`i' ) >= `=`yob'+`start'' & year(``type'_event'_date`i' ) <= `=`yob'+`end'' & dobyy == `yob'
								& substr(``type'_event'`i',``type'_cond',1) == "2" ;
						} ;		
						
					} ; /*end if */
				} ; /* end yob */
				
			} ;	/* end end */
		} ;	/* end start */
} ; /* end type */


foreach type in `type_list' { ;
forval age = `cum_range' { ;
	
		gen n_crime2`type'_cum`age' = . ;
		
		forval yob=`yob_min'(1)`yob_max' { ;

			if `=`yob'+`age'' <= `lastyr' { ;
			
				replace n_crime2`type'_cum`age' = 0 
						if (keep_`=`yob'+18'_`=`yob'+`age''_``type'_abbr' == 1) & dobyy == `yob' ;
				forval i=1(1)20 { ;
					replace n_crime2`type'_cum`age' = 1 
						if (keep_`=`yob'+18'_`=`yob'+`age''_``type'_abbr' == 1)
						& year(``type'_event'_date`i' ) >= `=`yob'+18' & year(``type'_event'_date`i' ) <= `=`yob'+`age'' & dobyy == `yob'
						& substr(``type'_event'`i',``type'_cond',1) == "2" ;
				} ;		
				
			} ; /*end if */
		} ; /* end yob */
					
		
		/* Total */
		gen n_crime2`type't_cum`age' = . ;
		
		forval yob=`yob_min'(1)`yob_max' { ;

			if `=`yob'+`age'' <= `lastyr' { ;
			
				replace n_crime2`type't_cum`age' = 0 
						if (keep_`=`yob'+18'_`=`yob'+`age''_``type'_abbr' == 1) & dobyy == `yob' ;
				forval i=1(1)20 { ;
					replace n_crime2`type't_cum`age' = n_crime2`type't_cum`age' + 1 
						if (keep_`=`yob'+18'_`=`yob'+`age''_``type'_abbr' == 1)
						& year(``type'_event'_date`i' ) >= `=`yob'+18' & year(``type'_event'_date`i' ) <= `=`yob'+`age'' & dobyy == `yob'
						& substr(``type'_event'`i',``type'_cond',1) == "2" ;
				} ;		
			
			} ; /*end if */
		} ; /* end yob */
		
} ; /* end age */	
} ; /* end type */
						
summ n_crime* ;

drop 
	first_name fssr_middlen last_name idu middle_name
	days_in_* event* day_of_week* 
	keep_* 
	;
	
save "`crime'\prep2\crime_allssr_prepforreg.dta", replace;

capture log close;

